package com.tl.sun.robust;

import android.content.Context;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.tl.sun.AppContext;
import com.tl.sun.OkHeader;
import com.tl.sun.manager.RobustVersionManager;
import com.tl.sun.model.RobustBean;

import org.json.JSONObject;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by apple on 17/4/25.
 */

public class HotFixPresenter {
    private JudgeIfHotFix mCallBack;

    public HotFixPresenter(JudgeIfHotFix callBack) {
        mCallBack = callBack;
    }

    public void getHotFixMessage(Context context){
        OkGo.getInstance().addCommonHeaders(OkHeader.getCommonHeadToken(context))
                .post(AppContext.getInstance().getBaseUrl()+"account/hot_update")
                .params("out_code", RobustVersionManager.getInstance().getvName())
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        try {
                            JSONObject jo=new JSONObject(s);
                            String ret=jo.getString("ret");
                            String code=jo.getString("code");
                            String msg=jo.getString("msg");
                            if (ret.equals("0")&&code.equals("1")){
                                RobustBean bean = new Gson().fromJson(s, RobustBean.class);
                                RobustBean.RetDataBean data = bean.getData();
                                mCallBack.onSuccess(data,msg);
                            } else {
                                mCallBack.onError(msg);
                            }
                        }catch (Exception e){}
                    }
                });
    }
}
